//
//  UserClient.m
//  HiMaster3
//
//  Created by jinghao on 16/4/15.
//  Copyright © 2016年 jinghao. All rights reserved.
//

#import "UserClient.h"


#define MASTER_SERVICE_NAME         @"com.hyutech.yihubang"
#define MASTER_RAW_LOGIN            @"RawLogin"
#define MASTER_LOGIN_Type           @"LoginType"
#define MASTER_LOGIN_Info           @"LoginInfo"
#define MASTER_PASSWORD             @"Password"
#define MASTER_USERNAME             @"UserName"
#define MASTER_ACCESS_TOKEN         @"AccessToken"
#define MASTER_LOGIN_USERINFO       @"userLoginInfo"
#define MASTER_CITY_CODE            @"city_code"
#define YIHUBANG_USERINFO           @"userInfo"
#define Order_Refund_Msg            @"order_refund_msg"
#define Enterprise_Course_Url       @"enterprise_course_url"
#define Master_Enter_Url            @"master_enter_url"
#define Financial_Data_Url          @"financial_data_url"
#define About_Us_Url                @"about_us_url"
#define About_Card_Url              @"about_card_url"
#define Agree_Url                   @"agree_url"
#define Activity_Url                @"activity_url"
#define Weixin_unionId              @"unionId"
#define User_OpenTypeId             @"openTypeId"
#define User_FirstContractName      @"firstContractName"
#define User_SetBaseInfo            @"setBaseInfo"

#define PATOO_STATE_USERINFO        @"userStateInfo"


@interface UserClient ()
@property (nonatomic,assign,readwrite)BOOL rawLogin;
@property (nonatomic,assign,readwrite)BOOL isMaster;
@property (nonatomic,strong, readwrite)NSString * openType;
@property (nonatomic,strong, readwrite)NSString * openTypeId;
@end

@implementation UserClient

+ (instancetype)sharedUserClient{
    static UserClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[UserClient alloc] init];
    });
    
    return _sharedClient;
}

- (id)init{
    if (self=[super init]) {

    }
    return self;
}

//- (BOOL)rawLogin
//{
//    return [[NSUserDefaults standardUserDefaults] boolForKey:MASTER_RAW_LOGIN];
//}

- (NSString*)userName
{
    return [SAMKeychain passwordForService:MASTER_SERVICE_NAME account:MASTER_USERNAME];
}

- (NSString*)password
{
    return [SAMKeychain passwordForService:MASTER_SERVICE_NAME account:MASTER_PASSWORD];
}

- (MasterLoginType)loginType{
    return [[NSUserDefaults standardUserDefaults] integerForKey:MASTER_LOGIN_Type];
}
-(NSString *)unionId{
    return [[NSUserDefaults standardUserDefaults] objectForKey:Weixin_unionId];
}

- (void)setLoginType:(MasterLoginType)type
{
    [[NSUserDefaults standardUserDefaults] setInteger:type forKey:MASTER_LOGIN_Type];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}
- (void)setLoginTypeName:(NSString*)typeName{
    if([typeName isEqualToString:@"qq"]){
        [self setLoginType:MasterLoginType_QQ];
    }else if([typeName isEqualToString:@"sina"]){
        [self setLoginType:MasterLoginType_Sina];
    }else if([typeName isEqualToString:@"wxsession"]){
        [self setLoginType:MasterLoginType_Weixin];
    }
}
- (void)setOpenTypeStatus:(NSArray *)type{
    if(type.count >0){
        self.openType =type[0];
//        self.openTypeId =type[1];
        [[NSUserDefaults standardUserDefaults] setObject:type[1] forKey:User_OpenTypeId];   
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}
- (void)setLoginInfo:(id)loginInfo
{
    [[NSUserDefaults standardUserDefaults] setObject:loginInfo forKey:MASTER_LOGIN_Info];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSDictionary*)loginInfo{
    return [[NSUserDefaults standardUserDefaults] objectForKey:MASTER_LOGIN_Info];
}
-(void) setUnionId:(NSString *)unionId{
    if(self.loginType ==MasterLoginType_Weixin){
        if(unionId){
            [[NSUserDefaults standardUserDefaults] setObject:unionId forKey:Weixin_unionId];
        }
    }
}
- (void)setUserName:(NSString *)userName password:(NSString *)password
{
    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:MASTER_RAW_LOGIN];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [SAMKeychain setPassword:password forService:MASTER_SERVICE_NAME account:MASTER_PASSWORD];
    [SAMKeychain setPassword:userName forService:MASTER_SERVICE_NAME account:MASTER_USERNAME];

}
- (void)updateSecurityCode:(NSString *)SecurityCode
{
    [SAMKeychain setPassword:SecurityCode forService:MASTER_SERVICE_NAME account:MASTER_PASSWORD];
}
- (void)updatephone:(NSString *)phone
{
    [SAMKeychain setPassword:phone forService:MASTER_SERVICE_NAME account:MASTER_USERNAME];
}
- (void)setUserLoginInfo:(id)userLoginInfo{
    _rawLogin = YES;
    _isMaster = [userLoginInfo[@"identity"] boolValue];
    if([userLoginInfo[@"identity"] isEqual:@"1"]){
        _isMaster=NO;
    }else{
        _isMaster=YES;
    }
    _userId = userLoginInfo[@"uid"];
    _accessToken = userLoginInfo[@"token"];
    [[NSUserDefaults standardUserDefaults] setObject:userLoginInfo forKey:MASTER_LOGIN_USERINFO];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

- (void)setUserInfo:(id)userInfo{
    _isMaster = [userInfo[@"identity"] boolValue];
    if([userInfo[@"identity"] isEqual:@"1"]){
        _isMaster=NO;
    }else{
        _isMaster=YES;
    }
    _userId = userInfo[@"uid"];
    [[NSUserDefaults standardUserDefaults] setObject:userInfo forKey:YIHUBANG_USERINFO];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}

- (void)setUserStateInfo:(id)userStateInfo {
    _userId = userStateInfo[@"uid"];
    [[NSUserDefaults standardUserDefaults] setObject:userStateInfo forKey:PATOO_STATE_USERINFO];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

//- (void)setUserproductInfo:(NSDictionary *)userproductInfo{
//    [[NSUserDefaults standardUserDefaults] setObject:userproductInfo forKey:User_ProductInfo];
//    [[NSUserDefaults standardUserDefaults] synchronize];
//    
//}
- (void)saveSetbaseInfo:(NSNumber *)setBaseInfo{
    [[NSUserDefaults standardUserDefaults] setObject:setBaseInfo forKey:User_SetBaseInfo];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}
-(NSNumber *)setBaseInfo{
    return [[NSUserDefaults standardUserDefaults] objectForKey:User_SetBaseInfo];
}
- (NSDictionary*)userInfo{
    return [[NSUserDefaults standardUserDefaults] objectForKey:YIHUBANG_USERINFO];
}



- (NSDictionary *)userStateInfo {
    return [[NSUserDefaults standardUserDefaults] objectForKey:PATOO_STATE_USERINFO];
}
//- (NSDictionary*)userproductInfo{
//    return [[NSUserDefaults standardUserDefaults] objectForKey:User_ProductInfo];
//}
- (NSDictionary*)userLoginInfo{
    return [[NSUserDefaults standardUserDefaults] objectForKey:MASTER_LOGIN_USERINFO];
}
-(NSString *)openTypeId{
    return [[NSUserDefaults standardUserDefaults] objectForKey:User_OpenTypeId];
}
- (void)outLogin{
    _rawLogin = NO;
    [self setLoginType:MasterLoginType_none];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:MASTER_RAW_LOGIN];
    [[NSUserDefaults standardUserDefaults] synchronize];

}
-(void)cancleHelp{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:User_OpenTypeId];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
-(NSString *)firstContractName{
    return [[NSUserDefaults standardUserDefaults] objectForKey:User_FirstContractName];
}
-(void)changeFirstContractName :(NSString *)name{
    [[NSUserDefaults standardUserDefaults] setObject:name forKey:User_FirstContractName];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSString*)city_code{
    NSString *cityCode = [[NSUserDefaults standardUserDefaults] objectForKey:MASTER_CITY_CODE];
    if (cityCode==nil) {
        cityCode = @"sh";
    }
    return cityCode;
}


- (void)setCity_code:(NSString*)type{
    [[NSUserDefaults standardUserDefaults] setValue:type forKey:MASTER_CITY_CODE];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}
@end
